package core.communication;

import java.net.MalformedURLException;

import org.jboss.remoting.InvokerLocator;

import core.communication.message.Message;
import core.database.NodeType;

public class Client {
	private org.jboss.remoting.Client client = null;
	private String locatorURI = null;
	private NodeType type = null;
	private Boolean isWorking = true;

	public Client(String locatorURI, NodeType type) {
		try {
			this.type = type;
			this.setLocatorURI(locatorURI);
			InvokerLocator invokerLocator;
			invokerLocator = new InvokerLocator("socket://" + locatorURI);
//			HashMap config = new HashMap();
//	        config.put(ServerInvoker.TIMEOUT, "2000");
			client = new org.jboss.remoting.Client(invokerLocator, locatorURI);
//	        client = new org.jboss.remoting.Client(invokerLocator, config);
			client.connect();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public org.jboss.remoting.Client getClient() {
		return this.client;
	}

	public String getLocatorURI() {
		return locatorURI;
	}

	public void setLocatorURI(String locatorURI) {
		this.locatorURI = locatorURI;
	}
	
	public NodeType getType() {
		return type;
	}

	public Boolean getIsWorking() {
		return isWorking;
	}

	public void setIsWorking(Boolean isWorking) {
		this.isWorking = isWorking;
	}
	
	public void send(Message message) throws Throwable {
		this.client.invoke(message);
	}
	
	public String toString() {
		return locatorURI + " "+type+" "+isWorking;
	}
}
